//
//  OADWKWebViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 5/9/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADWKWebViewController.h"
#import <WebKit/WebKit.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import "MBProgressHUD+MJ.h"
#import "OASDK.h"

@interface OADWKWebViewController ()
<
WKNavigationDelegate
>

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation OADWKWebViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    //
    [self.view addSubview:self.webView];
    
    self.strUrl = @"http://www.sohu.com/";
    self.strUrl = @"http://www.wheelpool.cn/answer.html";
    
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
//    NSURL *url = [NSURL URLWithString:self.strUrl];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    [self.webView loadData:data
//                  MIMEType:@"text/html"
//     characterEncodingName:@"UTF-8"
//                   baseURL:url];
    
    NSURL *url = [NSURL URLWithString:self.strUrl];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.strUrl]];
//    [self.webView loadRequest:request];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    //
    self.webView.oa_width = self.view.oa_width;
    self.webView.oa_height = self.view.oa_height;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods


#pragma mark - <WKNavigationDelegate>

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [MBProgressHUD hideHUDForView:self.view];
    
    [webView evaluateJavaScript:@"document.title" completionHandler:^(NSString *title, NSError * _Nullable error) {
        self.navigationItem.title = title;
        self.title = title;
    }];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.view];
}


#pragma mark - Setter

-(void)setStrUrl:(NSString *)strUrl{
    _strUrl = strUrl;
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]]];
}

#pragma mark - Getter

- (WKWebView *)webView {
    if (!_webView) {
        WKWebView *webView = [[WKWebView alloc] init];
        webView.navigationDelegate = self;
        
        _webView = webView;
    }
    return _webView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
